package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.SpecGroupDTO;
import com.leyou.common.dto.SpecParamDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpectMapper;
import com.leyou.item.service.ParamService;
import com.leyou.item.service.SpectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpectServiceImpl extends ServiceImpl<SpectMapper, SpecGroup> implements SpectService {
    //根据商品分类id,查询规格组的集合
    @Autowired
    private ParamService paramService;
    @Autowired
    private  SpectService spectService;
    @Override
    public List<SpecGroupDTO> findGroupByCategoryId(Long id) {
        List<SpecGroup> category_id = query().eq("category_id", id).list();
        return SpecGroupDTO.convertEntityList(category_id);
    }

    //根据分类id查询规格组及组内参数
    @Override
    public List<SpecGroupDTO> findGroupAndSpectByCategoryId(Long id) {
        //拿到组数据,因为返回的数据要进行DTO展示
        List<SpecGroup> specGroups = query().eq("category_id",id).list();
        List<SpecGroupDTO> specGroupDTOS = SpecGroupDTO.convertEntityList(specGroups);

        //判断分类下的规格组是否存在
        if(CollectionUtils.isEmpty(specGroupDTOS)){
            throw new LyException(404, "该分类下的规格组不存在！");
        }

        //查询到的参数转换成了集合,再把集合转换成DT
        List<SpecParam> specParamList = paramService.query().eq("category_id", id).list();
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(specParamList);
        //把查询到spectParamDTOs,对规格参数分组,groupId一致的分到一组
        Map<Long, List<SpecParamDTO>> longListMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getCategoryId));

        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            specGroupDTO.setParams(longListMap.get(specGroupDTO.getId()));
        }
        return specGroupDTOS;
    }

    //新增规格组
    @Override
    public void addParamGroup(SpecGroupDTO groupDTO) {
        //groupDTO开始转换spectGroup
        SpecGroup specGroup = groupDTO.toEntity(SpecGroup.class);
        spectService.save(specGroup);
        //spectService.save(groupDTO.toEntity(SpecGroup.class));
    }
   //修改规格组
    @Override
    public void postParamGroup(SpecGroupDTO groupDTO) {
        updateById(groupDTO.toEntity(SpecGroup.class));
    }
   //新增规格参数
    @Override
    public void addParam(SpecParamDTO specParamDTO) {
      paramService.save(specParamDTO.toEntity(SpecParam.class));
    }

     //修改规格参数
    @Override
    public void postParam(SpecParamDTO specParamDTO) {
        paramService.updateById(specParamDTO.toEntity(SpecParam.class));
    }
    //条件查询规格条件集合
    @Override
    public List<SpecParamDTO> queryParamList(Long categoryId, Long groupId, Boolean searching) {
        if(categoryId==null&&groupId==null){
            throw new LyException(400,"查询条件不能为空");
        }
        List<SpecParam> list = paramService.query()
                .eq(categoryId!=null,"category_id", categoryId)
                .eq(groupId!=null,"group_id", groupId)
                .eq(searching!=null,"searching", searching).list();
        return SpecParamDTO.convertEntityList(list);
    }


}
